<template>
  <div class="select-group" id="selectionSendForm">
    <el-form ref="form"
    :model="selectData"
    label-width="140px"
    label-position="right">
      <el-form-item :label="noticeTitle">
          <el-autocomplete :trigger-on-focus="false" :fetch-suggestions="querySearch" v-model="form.radioNotice" style="width:80%" :maxlength="200"></el-autocomplete>
          <el-popover
            placement="top"
            width="300"
            trigger="click"
            v-model="visible">
            <div class="pop-item" v-for="(item, idx) of selectData.yyData" :key="idx" @click="form.radioNotice = item ; visible = !visible">{{item}}</div>
            <el-button type="primary" slot="reference" v-show="selectData.yyData&&selectData.yyData.length>0">常用用语库</el-button>
          </el-popover>
      </el-form-item>
      <el-radio-group v-model="form.radio">
        <el-form-item label="函件办理环节" v-show="selectData.hjData&&selectData.hjData.length>0">
          <el-radio :label="item.value" v-for="(item, index) of selectData.hjData" :key="index">{{item.name}}</el-radio>
        </el-form-item>
        <el-form-item label="司领导处理环节" v-show="selectData.szData&&selectData.szData.length>0">
          <el-radio :label="item.value" v-for="(item, index) of selectData.szData" :key="index">{{item.name}}</el-radio>
        </el-form-item>
        <el-form-item label="处内人员" v-show="selectData.cnData&&selectData.cnData.length>0">
          <el-radio :label="item.value" v-for="(item, index) of selectData.cnData" :key="index">{{item.name}}</el-radio>
        </el-form-item>
      </el-radio-group>
      <el-form-item>
        <div style="width:80%">
          <el-button type="success" @click="onSubmit(0)" :loading="FormLoading">发送</el-button>
          <el-button v-show="backflag" type="danger" @click="onSubmit(1)" style="float:right" :loading="FormLoading">退回</el-button>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'select-group',
  props: ['selectData', 'selectPerson', 'backflag', 'selApi', 'selStep', 'substep'],
  data () {
    return {
      noticeTitle: '办理提示',
      form: {
        radio: '',
        radioNotice: ''
      },
      FormLoading: false,
      visible: false
    }
  },
  watch: {
    selectData (val, oldVal) {
      console.log('selectData has changed', val)
    },
    selApi (val, oldVal) {
      console.log('selApi has changed', val)
    },
    substep (val, oldVal) {
      console.log('substep has changed', val)
    },
    selectPerson (val, oldVal) {
      console.log('selectPerson has changed', val)
      this.form.radio = ''
    },
    backflag (val, oldVal) {
      console.log('backflag has changed', val)
    }
  },
  methods: {
    querySearch (queryString, cb) {
      return this.getNotice(this, queryString, cb)
    },
    getNotice (self, queryString, cb) {
      let para = {
        key: queryString
      }
      self.$http
        .post(this.$http.defaults.selectionAPI + `api/selection/home/getMatchedOpinions`, para)
        .then(function (response) {
          console.log('获取成功')
          var results = response.data.result
          let newArr = []
          results.forEach(function (item) {
            let obj = {'value': item}
            newArr.push(obj)
          })
          cb(newArr)
        })
        .catch(function (error) {
          console.log(error)
        })
    },
    onSubmit (flag) {
      if (this.selectPerson.length === 0) {
        this.$message({
          message: '请至少选择一个人选',
          type: 'error'
        })
        return
      }
      if (this.form.radio === '' && !flag) {
        this.$message({
          message: '请选择接受人',
          type: 'error'
        })
        return
      }
      let url = this.$http.defaults.selectionAPI + this.selApi
      // 获取所选发送人id
      console.log(JSON.stringify(this.selectPerson))
      let arr = []
      for (let i of this.selectPerson) {
        if (this.selStep === 3) {
          console.log('i.id')
          console.log(i.id)
          if (!i.id) {
            if (i.busCandidatePersonId) {
              i.id = i.busCandidatePersonId
            } else if (i.personId) {
              i.id = i.personId
            }
          }
          arr.push(i.id)
        } else if (this.selStep === 12) {
          console.log('i.id')
          console.log(i.id)
          if (!i.id) {
            if (i.busCandidatePersonId) {
              i.id = i.busCandidatePersonId
            } else if (i.personId) {
              i.id = i.personId
            }
          }
          arr.push(i.id)
        } else if (this.selStep === 4) {
          if (!i.id) {
            if (i.busCandidatePersonId) {
              i.id = i.busCandidatePersonId
            } else if (i.personId) {
              i.id = i.personId
            }
          }
          arr.push({
            id: i.id
          })
        } else if (this.selStep === 5) {
          let id = i.id
          if (!id) {
            id = i.personId
          }
          if (i.checkmsg) {
            arr.push({
              id: id,
              statePublicity: i.checkmsg.indexOf('1') > -1 ? 0 : -1,
              stateRecord: i.checkmsg.indexOf('2') > -1 ? 0 : -1
            })
          } else {
            arr.push({
              // statePublicity: i.statePublicity,
              // stateRecord: i.stateRecord,
              id: id
            })
          }
        } else if (this.selStep === 6) {
          console.log('i.jubao')
          console.log(i.jubao)
          if (this.substep === 1) {
            arr.push({
              id: i.personId
            })
          } else {
            if ((i.jubao === 0 || i.jubao === 1) && (i.yingxiang === 0 || i.yingxiang === 1)) {
              arr.push({
                id: i.personId,
                tips: i.jubao,
                appointment: i.yingxiang
              })
            } else {
              this.$message({
                message: '请选择每一位勾选人选的举报选项与任命选项',
                type: 'warning'
              })
              return
            }
          }
        } else if (this.selStep === 7 || this.selStep === 13) {
          if (this.substep === 1) {
            arr.push({
              id: i.personId
            })
          } else {
            if (i.passed !== 1 && i.passed !== 0) {
              this.$message({
                message: '请选择每一位勾选人选是否同意',
                type: 'warning'
              })
              return
            } else {
              arr.push({
                id: i.personId,
                passed: i.passed
              })
            }
          }
        } else if (this.selStep === 8) {
          arr.push({
            id: i.personId,
            arrivalTime: i.arrivalTime,
            probationPeriod: i.probationPeriod
          })
        }
      }
      console.log(url)
      console.log(arr)
      let opinion = ''
      let receiveUserId = 0
      if (flag) {
        receiveUserId = 0
        if (this.form.radioNotice === '') {
          opinion = '退回'
        } else {
          opinion = this.form.radioNotice
        }
      } else {
        receiveUserId = this.form.radio
        opinion = this.form.radioNotice
      }
      this.FormLoading = true
      if (this.selStep === 3 || this.selStep === 12) {
        this.$http.post(url, {
          receiveUserId: receiveUserId,
          personIds: arr,
          step: this.selStep,
          opinion: opinion
        }).then(res => {
          console.log('send succ res:', res)
          if (res.data.success === true) {
            this.$message({
              message: '发送成功',
              type: 'success'
            })
            this.FormLoading = false
            this.$emit('getList')
          }
        })
      } else if (this.selStep === 4 || this.selStep === 5 || this.selStep === 6 || this.selStep === 7 || this.selStep === 8 || this.selStep === 13) {
        console.log('arr')
        console.log(JSON.stringify(arr))
        this.$http.post(url, {
          receiveUserId: receiveUserId,
          persons: arr,
          step: this.selStep,
          opinion: opinion
        }).then(res => {
          console.log('send succ res:', res)
          if (res.data.success === true) {
            this.$message({
              message: '发送成功',
              type: 'success'
            })
            this.FormLoading = false
            this.$emit('getList')
          }
        })
      }
    }
  },
  mounted () {
    if (JSON.parse(localStorage.getItem('Roles')).indexOf('ROLE_sizhang') > -1 || JSON.parse(localStorage.getItem('Roles')).indexOf('ROLE_fusizhang') > -1) {
      this.noticeTitle = '批示意见'
    } else {
      this.noticeTitle = '办理提示'
    }
  }
}
</script>

<style>
#selectionSendForm .el-form-item{
  margin-bottom:10px !important
}
</style>
<style scoped>
.marginL8 {
  margin-left: 8px;
}
.select-group {
  width: 100%;
  margin-top: 15px;
}
.pop-item {
  line-height: 35px;
  font-size:var(--size16) !important;
  word-break: break-all;
  border-bottom: 1px solid #eee;
  cursor: pointer;
}
</style>
